package com.ruoyi.web.controller.business;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.dto.DtoWorkflow;
import com.ruoyi.system.service.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * OA回调Controller
 *
 * @author lxy
 * @date 2024-04-25
 */
@RestController
@RequestMapping("/business/oa")
public class TbOaController extends BaseController {

    @Autowired
    private ITbVisitorUserService visitorUserService;
    @Autowired
    private ITbVisitorVehicleService visitorVehicleService;
    @Autowired
    private ITbVehicleIllegalService vehicleIllegalService;
    @Autowired
    private ITbWeightTimeoutService tbWeightTimeoutService;
    @Autowired
    private ITbQrService tbQrService;
    @Autowired
    private IOaService oaService;

    /**
     * 审批结果通知
     */
    @PostMapping(value = "/workflowNotify")
    public AjaxResult workflowNotify(@RequestBody DtoWorkflow workflow)
    {
        AjaxResult ajax = AjaxResult.success();


        if ("1".equals(workflow.getType())) {
            // 访客
            TbVisitorUser user = visitorUserService.selectTbVisitorUserByFlowId(workflow.getRequestId());
            if (StringUtils.isNull(user)) {
                return AjaxResult.error("访客预约申请不存在！");
            }

            String status = workflow.getStatus();

            if ("2".equals(status)) {
                // 审核通过的场合
                // 获取二维码
                TbQr tbQr = tbQrService.selectTbQrLimit();

                // 二维码写入访客表
                user.setIsqr("1");
                user.setQr(tbQr.getCode());

                // 更新二维码表，该二维码已经被占用
                tbQr.setStatus("1");
                tbQr.setUpdateTime(DateUtils.getNowDate());
                tbQrService.updateTbQr(tbQr);
            }
            user.setStatus(workflow.getStatus());
            user.setReviewInfo(workflow.getRemark());
            user.setCheckTime(DateUtils.getNowDate());
            visitorUserService.updateTbVisitorUser(user);

            ajax.put("type", "1");
            ajax.put("returnId", user.getId());
        } else if ("2".equals(workflow.getType())) {
            // 车辆
            TbVisitorVehicle vehicle = visitorVehicleService.selectTbVisitorVehicleByFlowId(workflow.getRequestId());
            if (StringUtils.isNull(vehicle)) {
                return AjaxResult.error("访客车辆预约申请不存在！");
            }
            vehicle.setStatus(workflow.getStatus());
            vehicle.setReviewInfo(workflow.getRemark());
            vehicle.setCheckTime(DateUtils.getNowDate());
            visitorVehicleService.updateTbVisitorVehicle(vehicle);
            ajax.put("type", "2");
            ajax.put("returnId", vehicle.getId());
        } else if ("3".equals(workflow.getType())) {
            // 违章
            TbVehicleIllegal query = new TbVehicleIllegal();
            query.setRequestId(workflow.getRequestId());
            List<TbVehicleIllegal> illegalList = vehicleIllegalService.selectTbVehicleIllegalList(query);
            if (CollectionUtils.isEmpty(illegalList)) {
                return AjaxResult.error("车辆违章处理申请不存在！");
            }
            TbVehicleIllegal illegal = illegalList.get(0);
            illegal.setStatus(workflow.getStatus());
            illegal.setReviewInfo(workflow.getRemark());
            illegal.setCheckTime(DateUtils.getNowDate());

            if ("退回".equals(workflow.getRemark())) {
                // 罚单审核结果未【退回】的场合时，该条审核废弃【delete flag = 2】、并重新申请新的处罚流程

                TbVehicleIllegal newVehicleIllegal = new TbVehicleIllegal();
                String[] ignoreProperties = {"id","requestId","status", "reviewInfo", "checkTime"};
                BeanUtils.copyProperties(illegal, newVehicleIllegal, ignoreProperties);
                String result = oaService.createVehicleIllegalWorkflow(illegal);
                // 解析返回结果
                JSONObject json = JSONObject.parseObject(result);
                if ("100".equals(json.getString("code"))) {
                    JSONObject data = json.getJSONObject("data");
                    newVehicleIllegal.setRequestId(data.getString("requestid"));
                    vehicleIllegalService.insertTbVehicleIllegal(newVehicleIllegal);
                }

                illegal.setDelFlag("2");
            }
            vehicleIllegalService.updateTbVehicleIllegal(illegal);
            ajax.put("type", "3");
            ajax.put("returnId", illegal.getId());
        } else if ("4".equals(workflow.getType())) {
            // 过磅超时
            TbWeightTimeout query = new TbWeightTimeout();
            query.setRequestId(workflow.getRequestId());
            List<TbWeightTimeout> timeoutList = tbWeightTimeoutService.selectTbWeightTimeoutList(query);
            if (CollectionUtils.isEmpty(timeoutList)) {
                return AjaxResult.error("过磅超时处理申请不存在！");
            }
            TbWeightTimeout timeout = timeoutList.get(0);
            timeout.setStatus(workflow.getStatus());
            timeout.setReviewInfo(workflow.getRemark());
            timeout.setCheckTime(DateUtils.getNowDate());

            tbWeightTimeoutService.updateTbWeightTimeout(timeout);
            ajax.put("type", "4");
            ajax.put("returnId", timeout.getId());
        }
        return ajax;
    }
}
